package com.polaris.live.ui.welcome

import android.content.Intent
import android.os.Bundle
import android.os.CountDownTimer
import com.blankj.utilcode.util.TimeUtils
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.mvvm.base2.BaseVbActivity
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.util.ImmersionBarBuilder
import com.polaris.live.databinding.ActivityDiscontinuedBinding
import com.polaris.live.resp.back_resp.DiscontinuedExt
import kotlin.system.exitProcess

/**
 * 停服公告
 *
 * @author Created by 半仙 on 2023/8/9/009 11:12
 */
class DiscontinuedActivity : BaseVbActivity<BaseViewModel, ActivityDiscontinuedBinding>() {

    private var data: DiscontinuedExt? = null

    private var time: TimeCount? = null

    override fun ImmersionBarBuilder.buildImmersionBar() {
        setStatusBarDarkFont(false)
        setTitleBar(mBinding.llTitle)
    }

    override fun initView(savedInstanceState: Bundle?) {

        data = intent.getParcelableExtra("data")

        mBinding.tvMessage.text = data?.message

        val startTime = data?.startTime
        if (startTime != null) {
            time = TimeCount(startTime, 1000)
            time?.start()
        }

        mBinding.outApp.setOnSingleClickListener {
            val intent = Intent(Intent.ACTION_MAIN)
            intent.addCategory(Intent.CATEGORY_HOME)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            startActivity(intent)
            finishAffinity()
            exitProcess(0)
        }

        //TODO 后续添加逻辑 如果每30调一次preconfig 如果成功了就走正常的逻辑如果没有则不管
    }

    internal inner class TimeCount(millisInFuture: Long, countDownInterval: Long) :
        CountDownTimer(millisInFuture, countDownInterval) {

        override fun onTick(millisUntilFinished: Long) {
            val millis2String = TimeUtils.millis2String(millisUntilFinished, "HH:mm:ss")
            mBinding.tvTime.text = millis2String
        }

        override fun onFinish() {

        }
    }

}